import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeView from '../views/HomeView.vue'//主页所以是静态导入,开始就导入
//import LoginView from '../views/LoginView.vue'//主页所以是静态导入,开始就导入
import cookie from '../util/cookie'
Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'tiyu',
    component: HomeView,
    redirect:'/tiyu',
    children:[
      {
        path: '/tiyu',
        component: () => import(
            '../views/tiyuju/tiyuguanliminIndex.vue')
      },

      {
        /*申报信息审核-基地申报审核*/
        path: '/tiyuju/information-moderation/baseaudit',
        component: () => import(
            '../views/tiyuju/information-moderation/BaseAudit.vue')
      },
      {
        /*申报信息审核-项目申报审核*/
        path: '/tiyuju/information-moderation/projectaudit',
        component: () => import(
            '../views/tiyuju/information-moderation/ProjectAudit.vue')
      },
      {
        /*申报信息审核-单位申报审核*/
        path: '/tiyuju/information-moderation/Unitaudit',
        component: () => import(
            '../views/tiyuju/information-moderation/UnitAudit.vue')
      },
      /*用户管理*/
      {
        path: '/tiyuju/information-moderation/Usermanagement',
        component: () => import(
            '../views/tiyuju/information-moderation/UserManagement.vue')
      },


      {
        /*申报用户管理*/
        path: '/user/UserManage',
        component: () => import(
            '../views/user/UserManage.vue')
      },
      {
        /*申报信息审核-单位申报审核*/
        path: '/tiyuju/information-moderation/roleList',
        component: () => import(
            '../views/tiyuju/information-moderation/roleList.vue')
      },
      {
        /*项目申报-示范单位申报信息*/
        path: '/tiyuju/project-declaration/SUnitDeclaration',
        component: () => import(
            '../views/tiyuju/project-declaration/SUnitDeclaration.vue')
      },
      {
        /*项目申报-示范项目申报信息*/
        path: '/tiyuju/project-declaration/projectaudit',
        component: () => import(
            '../views/tiyuju/project-declaration/SProjectDeclaration.vue')
      },
      {
        /*项目申报-示范基地申报信息*/
        path: '/tiyuju/project-declaration/baseaudit',
        component: () => import(
            '../views/tiyuju/project-declaration/SBaseDeclaration.vue')
      },
      {
        /*内部单位*/
        path: '/tiyuju/internal_unit',
        component: () => import(
            '../views/tiyuju/internal_unit/InternalUnitView.vue')
      },
      {
        /*内部单位*/
        path: '/tiyuju/userLogLogin',
        component: () => import(
            '../views/tiyuju/internal_unit/UserLogLoginlUnitView.vue')
      },
      /*公告*/
      {
        path: '/notification-announcement',
        component: () => import( '../views/Notification announcement')
      },

    ]
  },
  {
    path: '/about',
    name: 'about',
    component: () => import( '../views/AboutView.vue')

  },
  {
    path: '/reg',
    name:'login',
    component: () => import('../views/RegView.vue'),
    meta:{
      requireAuth:false
    }
  },
  {
    path: '/Login',
    name:'login',
    component: () => import('../views/LoginView.vue'),
    meta:{
      requireAuth:true
     }
  },
  {
    path: '/youxiangLogin',
    name:'youxiangLogin',
    component: () => import('../views/youxiangLoginView.vue'),
    meta:{
      requireAuth:true
    }
  },
  {
    path: '/shoujiLogin',
    name:'shoujiLogin',
    component: () => import('../views/shoujiLoginView.vue'),
    meta:{
      requireAuth:true
    }
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})


/*
* beforeEach:从一个页面跳转到另外一个页面时触发
* to:要跳转的页面
* from:从哪个页面出来
* next:决定是否通过
*/
//if (window.location.href !== "http://localhost:9060/reg") {
router.beforeEach((to, from, next) => {
  // 如果跳转的页面不存在，跳转到404页面
    if(to.matched.length===0){
      next('/404')
    }
    if (cookie.getCookie("openId")) {
      next()
    } else {
      if (to.path === "/Login") {
        next()
      } else if(to.path === "/shoujiLogin") {
        next()
      } else if(to.path === "/youxiangLogin") {
        next()
      } else if(to.path === "/reg") {
        next()
      }
      else
      {
        next('/Login')
      }

    }
})

export default router
